<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var findMatrix = function (nums) {
      const res = []
      for (const num of nums) {
        let l = 0
        while (res[l] && res[l].includes(num)) l++
        res[l] = res[l] || []
        res[l].push(num)
      }
      return res
    };
    console.log(findMatrix(nums = [1, 3, 4, 1, 2, 3, 1]))
  </script>
</body>

</html>